package com.ruoyi.carton.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.carton.domain.Box;
import com.ruoyi.carton.mapper.BoxMapper;
import com.ruoyi.carton.service.BoxService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author 时空
* @description 针对表【box(纸箱产品信息表)】的数据库操作Service实现
* @createDate 2025-06-28 13:26:32
*/
@Service
public class BoxServiceImpl extends ServiceImpl<BoxMapper, Box>
    implements BoxService{

    @Override
    public List<Box> selectBoxList(Box box) {
        return this.lambdaQuery()
                .like(StringUtils.isNotBlank(box.getProductName()), Box::getProductName, box.getProductName())
                .list();
    }

    @Override
    public boolean addBox(Box box) {
        // ID不能为空
        if(StringUtils.isBlank(box.getId())){
            throw new ServiceException("ID不能为空");
        }
        //判断id是否存在
        if(this.getById(box.getId()) != null){
            throw new ServiceException("ID已存在");
        }

        if(box.getSalePrice()!= null){
            if(box.getRebate()!= null){
                box.setProfit(box.getSalePrice().subtract(box.getRebate()));
            }
            else {
                box.setRebate(BigDecimal.ZERO);
                box.setProfit(box.getSalePrice());
            }
        }else {
            box.setSalePrice(BigDecimal.ZERO);
            box.setRebate(BigDecimal.ZERO);
            box.setProfit(BigDecimal.ZERO);
        }
        return this.save(box);
    }
}




